package com.qqtech.qquan.community.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.community.dao.CommunitySignDao;
import com.qqtech.qquan.community.model.CommunitySign;
import com.qqtech.qquan.community.model.CommunitySignQuery;

@Service
public class CommunitySignServiceImpl extends BaseServiceImpl<CommunitySign>
		implements CommunitySignService {
	public static final Logger log = LoggerFactory.getLogger(CommunitySignServiceImpl.class);
	
	@Resource
	private CommunitySignDao communitySignDao;

	@Override
	protected BaseDao<CommunitySign> getBaseDao() {
		return communitySignDao;
	}

	@Override
	public boolean isSign(Integer userId, Integer communityId) {
		boolean flag=false;
		CommunitySignQuery communitySignQuery=new CommunitySignQuery();
		communitySignQuery.setUserId(userId);
		communitySignQuery.setCommunityId(communityId);
		communitySignQuery.setNewAt(TimeUtil.now());
		int count=communitySignDao.queryCount(communitySignQuery);
		if(count>0){
			flag=true;
		}
		return flag;
	}
	
	@Override
	public Integer myLastSignCommunity(Integer userId) {
		CommunitySignQuery communitySignQuery=new CommunitySignQuery();
		communitySignQuery.setUserId(userId);		
		Pageable pageKit = new PageKit(1,1, new PageOrder(Direction.DESC, "newAt"));
		List<CommunitySign> comSignList = communitySignDao.queryList(communitySignQuery, pageKit);
		if(comSignList!=null && comSignList.size()>0){
			CommunitySign sign = comSignList.get(0);
			if(sign!=null){
				return sign.getCommunityId();
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
}